# coding: utf-8
from PySide6.QtCore import Qt, Signal
from PySide6.QtGui import QColor
from PySide6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout
from qfluentwidgets import themeColor, IconWidget, SubtitleLabel, TransparentPushButton, FluentIcon, BodyLabel

from common import SingleIcon, cfg
from components import GridLayout, VideoCard


class ClassicHeaderCardWidget(QWidget):
    moreSignal = Signal(object)

    def __init__(self, title: str, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.horizontalLayout = QHBoxLayout()
        self.gridLayout = GridLayout()
        icon = SingleIcon.VERTICAL_LINE.icon(color=themeColor())
        self.iconLabel = IconWidget(icon, self)
        self.titleLabel = SubtitleLabel(title, self)
        self.contentLabel = BodyLabel('', self)
        self.moreButton = TransparentPushButton(FluentIcon.MORE, '更多', self)

        self.iconLabel.setFixedSize(24, 24)
        self.gridLayout.setItemMinimumWidth(300)
        self.contentLabel.setTextColor(QColor(0, 0, 0, 150), QColor(255, 255, 255, 150))
        self.setMinimumHeight(260)

        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setSpacing(10)
        self.horizontalLayout.setSpacing(10)
        self.horizontalLayout.addWidget(self.iconLabel)
        self.horizontalLayout.addWidget(self.titleLabel)
        self.horizontalLayout.addWidget(self.contentLabel)
        self.horizontalLayout.addStretch(1)
        self.horizontalLayout.addWidget(self.moreButton)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.verticalLayout.addLayout(self.gridLayout)

        self.moreButton.setVisible(False)
        self.moreButton.clicked.connect(lambda s: self.moreSignal.emit(self.property('spid')))
        cfg.themeColorChanged.connect(self.updateTheme)

    def load(self, vodrows: list):
        for vodrow in vodrows:
            if vodrow['isvip'] != '0':
                continue
            card = VideoCard(self)
            card.setData(vodrow)
            self.gridLayout.addWidget(card)
        if self.count() == 0:
            self.setVisible(False)

    def clear(self) -> None:
        self.gridLayout.takeAllWidgets()

    def count(self) -> int:
        return self.gridLayout.count()

    def setMoreButtonVisible(self, visible: bool) -> None:
        self.moreButton.setVisible(visible)

    def updateTheme(self, color: str) -> None:
        self.iconLabel.setIcon(SingleIcon.VERTICAL_LINE.icon(color=color))

    def setContent(self, content: str) -> None:
        self.contentLabel.setText(content)

    def setTitle(self, title: str) -> None:
        self.titleLabel.setText(title)
